﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using System.IO;
using BLL;
using Public;
using System.Windows.Forms;
//using System.Windows.Forms.FileDialog;
//using System.Windows.Forms.SaveFileDialog;
//using System.Nullable;


namespace QLNhanKhau
{
    public partial class Report : Window
    {
        ReportViewer viewer = new ReportViewer();

        public Report(DateTime BD, DateTime KT, bool bprint = false)
        {
            //InitializeComponent();

            viewer.LocalReport.ReportPath = "../../Report.rdlc";
            List<SoHoKhauPublic> names = new List<SoHoKhauPublic>();

            if (bprint)
                viewer.RenderingComplete += new RenderingCompleteEventHandler(viewer_RenderingComplete);
            
            //Person[] nms = { new Person("JOHN"), new Person("JAC"), new Person("HOMER") };
            //names.AddRange(nms);
            names = SoHoKhauBLL.BaoCaoSHK(BD,KT);
           // for (int i = 0; i < 60; i++)
            //    names.AddRange(nms);
            viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
        }
        public Report(string TenFile, bool bprint=false)
        {
             viewer.LocalReport.ReportPath = "../../" + TenFile + ".rdlc";
            

            if (bprint)
                viewer.RenderingComplete += new RenderingCompleteEventHandler(viewer_RenderingComplete);
            
            //Person[] nms = { new Person("JOHN"), new Person("JAC"), new Person("HOMER") };
            //names.AddRange(nms);
            if (TenFile == "NVQS")
            {
                List<NhanKhauPublic> names = new List<NhanKhauPublic>();
                names = NhanKhauBLL.NVQS();
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            if (TenFile == "TrinhDo")
            {
                List<TrinhDoPublic> names = new List<TrinhDoPublic>();
                names = TrinhDoBLL.ReportTD();
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            if (TenFile == "ChuyenKhau")
            {
                List<ChuyenKhauPublic> names = new List<ChuyenKhauPublic>();
                
                //names = NhanKhauDataSet.
                viewer.LocalReport.DataSources.Add(new ReportDataSource("Dataset1", names));
            }
            
           // for (int i = 0; i < 60; i++)
            //    names.AddRange(nms);
            
        }
        
        void OnRenderingCompletePrintOnly(object sender, System.ComponentModel.AsyncCompletedEventArgs args)
        {
            object objviewer = viewer;
            object[] prms = { sender, args };
            PrintReportViewer.ExecuteFunction(objviewer, prms, "OnRenderingCompletePrintOnly");
        }
        bool streamed = false;
        Stream CreateStreamEMFPrintOnly(string name, string extension, Encoding encoding, string mimeType, bool useChunking, Microsoft.ReportingServices.Interfaces.StreamOper operation)
        {
            if (streamed)
                return null;
            streamed = true;
            object objviewer = viewer;
            object[] prms = { name, extension, encoding, mimeType, useChunking, operation };
            Stream str = (Stream)PrintReportViewer.ExecuteFunction(objviewer, prms, "CreateStreamEMFPrintOnly");
            return str;
        }
        public Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream CreateAndRegisterStream
        {
            get
            {
                return new Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream(CreateStreamEMFPrintOnly);
            }
        }
        public System.ComponentModel.AsyncCompletedEventHandler AsyncCompletedEventHandler
        {
            get
            {
                return new System.ComponentModel.AsyncCompletedEventHandler(this.OnRenderingCompletePrintOnly);
            }
        }
        public void Print()
        {

            viewer.RefreshReport();

        }
        public void PrintByPrinter(string Printername)
        {
            PrintReportViewer.PrintByPriner(this, viewer, Printername);

        }
        void viewer_RenderingComplete(object sender, RenderingCompleteEventArgs e)
        {
            PrintReportViewer.PrintwithDialog(viewer);
        }
        public void Savereport(string name)
        {
            string _sPathFilePDF = String.Empty;
            String v_mimetype;
            String v_encoding;
            String v_filename_extension;
            String[] v_streamids;
            Microsoft.Reporting.WinForms.Warning[] warnings;
            string _sSuggestedName = name;

            //Microsoft.Reporting.WinForms.ReportViewer reportViewer1;
            Microsoft.Reporting.WinForms.LocalReport objRDLC = new Microsoft.Reporting.WinForms.LocalReport();
            viewer.LocalReport.ReportEmbeddedResource = "'"+name+"'"+".rdlc";
            viewer.LocalReport.DisplayName = _sSuggestedName;

            objRDLC.DataSources.Clear();
            byte[] byteViewer = viewer.LocalReport.Render("PDF", null, out v_mimetype, out v_encoding, out v_filename_extension, out v_streamids, out warnings);

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "*PDF files (*.pdf)|*.pdf";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = _sSuggestedName;
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            //{
                FileStream newFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                newFile.Write(byteViewer, 0, byteViewer.Length);
                newFile.Close();
            //}
        }
       

    }
}
